"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var _require = require('../utils'),
  mapInfoResource = _require.mapInfoResource,
  findApkIconPath = _require.findApkIconPath;
var BinaryXmlParser = require('./binary');
var ICON_NODE_NAMES = ['foreground', 'background', 'monochrome'];
var AdaptiveIconParser = /*#__PURE__*/function () {
  function AdaptiveIconParser(buffer, resourceMap) {
    var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    _classCallCheck(this, AdaptiveIconParser);
    this.buffer = buffer;
    this.resourceMap = resourceMap;
    this.xmlParser = new BinaryXmlParser(this.buffer, options);
  }
  _createClass(AdaptiveIconParser, [{
    key: "parse",
    value: function parse() {
      var _this = this;
      var document = this.xmlParser.parse();
      var adaptiveIcon = {};
      if (document.nodeName === 'adaptive-icon') {
        document.childNodes.forEach(function (element) {
          if (ICON_NODE_NAMES.includes(element.nodeName)) {
            adaptiveIcon[element.nodeName] = _this.parseAdaptiveIconElement(element);
          }
        });
        return adaptiveIcon;
      }
      return null;
    }
  }, {
    key: "parseAdaptiveIconElement",
    value: function parseAdaptiveIconElement(element) {
      var collapsed = Object.create(null);
      for (var _i = 0, _Array$from = Array.from(element.attributes); _i < _Array$from.length; _i++) {
        var attr = _Array$from[_i];
        collapsed[attr.name] = attr.typedValue.value;
      }
      var resource = mapInfoResource(collapsed, this.resourceMap);
      if (resource.drawable) {
        return findApkIconPath({
          application: {
            icon: resource.drawable
          }
        });
      }
      return '';
    }
  }]);
  return AdaptiveIconParser;
}();
module.exports = AdaptiveIconParser;